import sys
from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QWidget, QApplication, QFileDialog, QGridLayout
from mat import Ui_Form
import numpy as np
import pandas as pd
import matplotlib
matplotlib.use("Qt5Agg")
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from prdct import predict_spec

class MyFigure(FigureCanvas):
    def __init__(self, width, height, dpi):
        self.fig = plt.figure(figsize=(width, height), dpi=dpi)
        super(MyFigure, self).__init__(self.fig)
        self.axes = self.fig.add_subplot(111)

class MainWindow(QWidget, Ui_Form):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUi(self)
        self.F = MyFigure(width=3, height=2, dpi=100)
        self.fileName = ""
        self.whitename = ""

    def draw_pic(self):
        tmp = pd.read_csv(self.fileName, sep='\t', header=None)
        x_axis = np.array(tmp[0])
        x_axis = x_axis[200:1100]
        w = pd.read_csv(self.whitename, sep='\t', header=None)
        white = np.array(w[1])
        white = white[200:1100]
        y_axis = np.array(tmp[1])
        y_axis = y_axis[200:1100]
        # light = pd.read_csv(r"D:\Programming\graduation_proj_CXL\cxl_grad\testwhite\light.txt", sep='\t', header=None)
        # wht = pd.read_csv(r"D:\Programming\graduation_proj_CXL\cxl_grad\testwhite\wht.txt", sep='\t', header=None)
        # white_light = np.array(wht[1])
        # white_light = white_light[200:1000]
        # lt = np.array(light[1])
        # lt = lt[200:1000]
        # reflct_wh = white_light / lt
        y_axis = y_axis / white * 0.97

        self.F.axes.cla()
        self.F.axes.plot(x_axis, y_axis)
        self.F.fig.suptitle("pic")
        self.F.draw()
        self.gridlayout = QGridLayout(self.groupBox)
        self.gridlayout.addWidget(self.F)
        self.F.flush_events()

    def make_prediction(self):
        result = predict_spec(self.fileName, self.whitename, self.h5_file.toPlainText())
        self.result.setText("result: "+str(result[0][0]))
        if(result[0][0] >= 0.5):
            pic = QtGui.QPixmap(r"1.jpg").scaled(self.pic_car.width(), self.pic_car.height())
            self.pic_car.setPixmap(pic)
        else:
            pic = QtGui.QPixmap(r"0.jpg").scaled(self.pic_car.width(), self.pic_car.height())
            self.pic_car.setPixmap(pic)
        self.draw_pic()

    def choose_file(self):
        self.fileName, fileType = QFileDialog.getOpenFileName(None, "选取文件", "C:\\", "Text Files(*.txt)")
        self.spec_file.setText(self.fileName)

    def choose_white(self):
        self.whitename, fileType = QFileDialog.getOpenFileName(None, "选取文件", "C:\\", "Text Files(*.txt)")
        self.whitefile.setText(self.whitename)

    def choose_h5(self):
        file_h5, filetype = QFileDialog.getOpenFileName(None, "选取文件", "C:\\", "h5 Files(*.h5)")
        self.h5_file.setText(file_h5)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = MainWindow()
    win.setWindowTitle("Classify")
    win.show()
    sys.exit(app.exec_())
